<?php defined('BASEPATH') OR exit('No direct script access allowed');

class login extends wsBackend
{
    public function __construct()
    {
        parent::__construct();
        if ($this->user->is_admin()) {
            redirect(admin_url());
        }
    }

    public function index()
    {
        $data = array();

        $this->tpl->meta ['title'] = '管理登陆 - ' . $this->config->item('site_name');
        $this->tpl->meta ['description'] = '';
        $this->tpl->meta ['keywords'] = '';

        $this->load->library('form_validation');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'required');

        if ($this->form_validation->run()) {
            $user = $this->user_model->validateLogin($this->input->post('email'), $this->input->post('password'));
            if ($user) {
                $this->session->set_userdata('LAST_LOGIN', $user->user_lastLogin);
                $this->user->setUser($user, isset($_POST['remember_me']) ? true : false);
                redirect(admin_url('dashboard'));
            } else {
                $this->session->set_flashdata('ERROR', '输入有效的帐号和密码');
                redirect(admin_url('login'));
            }
        }

        $this->tpl->contents [] = $this->tpl->fetch('contents/login', $data);
        $this->tpl->render('layouts/login');
    }
}
